﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace PIM_ATE.Mods
{
    public class ClsDataPIMTestForMOD
    {
        //<ProductSN>string</ProductSN>
        public string ProductSN { get; set; }
        //<TestTime>dateTime</TestTime>
        public DateTime TestTime { get; set; }
        //<ProductTypeID>string</ProductTypeID>
        public string ProductTypeID { get; set; }
        //<UserID>string</UserID>
        public string UserID { get; set; }
        //<PCIP>string</PCIP>
        public string PCIP { get; set; }
        //<SignalSource1>string</SignalSource1>
        public string SignalSource1 { get; set; }
        //<SignalSource2>string</SignalSource2>
        public string SignalSource2 { get; set; }
        //<SpectrumAnalyzer>string</SpectrumAnalyzer>
        public string SpectrumAnalyzer { get; set; }
        //<TestStep>string</TestStep>
        public string TestStep { get; set; }
        //<IsPass>string</IsPass>
        public string IsPass { get; set; }
        //<MoreContent>string</MoreContent>
        public string MoreContent { get; set; }
    }

    public class PIMTestItemInfo
    {
        //<ProductSN>string</ProductSN>
        public string ProductSN { get; set; }
        //<TestTime>dateTime</TestTime>
        public DateTime TestTime { get; set; }
        //<TopoAlias>string</TopoAlias>
        public string TopoAlias { get; set; }
        //<InterceptOrder>string</InterceptOrder>
        public string InterceptOrder { get; set; }
        //<InputPower1>decimal</InputPower1>
        public decimal InputPower1 { get; set; }
        //<InputPower2>decimal</InputPower2>
        public decimal InputPower2 { get; set; }
        //<InputF1>decimal</InputF1>
        public decimal InputF1 { get; set; }
        //<InputF2>decimal</InputF2>
        public decimal InputF2 { get; set; }
        //<OutputF>decimal</OutputF>
        public decimal OutputF { get; set; }
        //<TestValue>decimal</TestValue>
        public decimal TestValue { get; set; }
        //<HighSpec>decimal</HighSpec>
        public decimal HighSpec { get; set; }
        //<IsPass>string</IsPass>
        public string IsPass { get; set; }
    }

    public class tb_Result_DFL_Info
    {
        //<resultid>int</resultid>
        public int resultid { get; set; }
        //<partcode>string</partcode>
        public string partcode { get; set; }
        //<testtime>dateTime</testtime>
        public DateTime testtime { get; set; }
        //<testperiod>int</testperiod>
        public int testperiod { get; set; }
        //<testequipid>string</testequipid>
        public string testequipid { get; set; }
        //<projname>string</projname>
        public string projname { get; set; }
        //<projver>string</projver>
        public string projver { get; set; }
        //<tfpid>string</tfpid>
        public string tfpid { get; set; }
        //<userid>string</userid>
        public string userid { get; set; }
        //<totalresult>string</totalresult>
        public string totalresult { get; set; }
        //<itemresult>string</itemresult>
        public string itemresult { get; set; }
        //<itemselinfo>string</itemselinfo>
        public string itemselinfo { get; set; }
        //<itemselcnt>int</itemselcnt>
        public int itemselcnt { get; set; }
        //<itemcnt>int</itemcnt>
        public int itemcnt { get; set; }
        //<remark>string</remark>
        public string remark { get; set; }
        //<IsPIM>boolean</IsPIM>
        public bool IsPIM { get; set; }
    }

    public class tb_Param_DFL_Info
    {
        //<resultid>int</resultid>
        public int resultid { get; set; }
        //<partcode>string</partcode>
        public string partcode { get; set; }
        //<Testequipid>string</Testequipid>
        public string Testequipid { get; set; }
        //<ProcessNum>int</ProcessNum>
        public int ProcessNum { get; set; }
        //<UnitName>string</UnitName>
        public string UnitName { get; set; }
        //<testtime>dateTime</testtime>
        public DateTime testtime { get; set; }
        //<ParamName>string</ParamName>
        public string ParamName { get; set; }
        //<Res>int</Res>
        public int Res { get; set; }
        //<ResValue>string</ResValue>
        public string ResValue { get; set; }
        //<ResDesc>string</ResDesc>
        public string ResDesc { get; set; }
    }
    public class testInfo
    {
        public string TestBand { get; set; }
        public bool Flag { get; set; }
    }

    public class UpdatatestInfo : ClsDataPIMTestForMOD
    {
        public string TestBand { get; set; }
        public bool Flag { get; set; }

    }


    public class TestDataInfo
    {
        public string TestBand { get; set; }
        public bool Flag { get; set; }
        //<ProductSN>string</ProductSN>
        public string ProductSN { get; set; }
        //<TestTime>dateTime</TestTime>
        public DateTime TestTime { get; set; }
        //<ProductTypeID>string</ProductTypeID>
        public string ProductTypeID { get; set; }
        //<UserID>string</UserID>
        public string UserID { get; set; }
        //<PCIP>string</PCIP>
        public string PCIP { get; set; }
        //<SignalSource1>string</SignalSource1>
        public string SignalSource1 { get; set; }
        //<SignalSource2>string</SignalSource2>
        public string SignalSource2 { get; set; }
        //<SpectrumAnalyzer>string</SpectrumAnalyzer>
        public string SpectrumAnalyzer { get; set; }
        //<TestStep>string</TestStep>
        public string TestStep { get; set; }
        //<IsPass>string</IsPass>
        public string IsPass { get; set; }
        //<MoreContent>string</MoreContent>
        public string MoreContent { get; set; }

        public TestDataInfo(UpdatatestInfo ui)
        {
            TestBand = ui.TestBand;
            Flag = ui.Flag;
            ProductSN = ui.ProductSN;
            TestTime = ui.TestTime;
            ProductTypeID = ui.ProductTypeID;
            UserID = ui.UserID;
            PCIP = ui.PCIP;
            SignalSource1 = ui.SignalSource1;
            SignalSource2 = ui.SignalSource2;
            SpectrumAnalyzer = ui.SpectrumAnalyzer;
            TestStep = ui.TestStep;
            IsPass = ui.IsPass;
            MoreContent = ui.MoreContent;
        }
        public TestDataInfo()
        { }
    }

    class ClsDataResultForMOD
    {
        public static string url = "http://10.53.20.170/pa/WebService/ServiceForVendor/PIM_TestAndDataUpload_Service.asmx";
        MicroWebHelper m_webObj;

        public ClsDataResultForMOD()
        {
            m_webObj = new MicroWebHelper();
        }

        public string GetTestStep_bySN(string sn, ModTestMode mode = ModTestMode.First)
        {
            Hashtable pairs = new Hashtable();
            pairs.Add("productSN", sn);
            
            XmlDocument doc = m_webObj.QuerySoapWebService(url, "GetTestStep_bySN", pairs);
            if (doc == null) return null;
            return doc.GetElementsByTagName("result")[0].InnerText;
        }
        public string GetTestStep_ForPIMSecondTest_bySN(string sn)
        {
            Hashtable pairs = new Hashtable();
            pairs.Add("productSN", sn);

            XmlDocument doc = m_webObj.QuerySoapWebService(url, "GetTestStep_ForPIMSecondTest_bySN", pairs);
            if (doc == null) return null;
            return doc.GetElementsByTagName("result")[0].InnerText;
        }

        public string ProductTypeID_GetbySN_FromDebugTest(string sn)
        {
            Hashtable pairs = new Hashtable();
            pairs.Add("productSN", sn);

            XmlDocument doc = m_webObj.QuerySoapWebService(url, "ProductTypeID_GetbySN_FromDebugTest", pairs);
            if (doc == null) return null;
            return doc.GetElementsByTagName("result")[0].InnerText;
        }

        public string ProductTypeName_GetbyProductTypeID(string id)
        {
            Hashtable pairs = new Hashtable();
            pairs.Add("productTypeID", id);

            XmlDocument doc = m_webObj.QuerySoapWebService(url, "ProductTypeName_GetbyProductTypeID", pairs);
            if (doc == null) return null;
            return doc.GetElementsByTagName("result")[0].InnerText;
        }

        public bool IsValidProductTypeID(string id)
        {
            Hashtable pairs = new Hashtable();
            pairs.Add("productTypeID", id);

            XmlDocument doc = m_webObj.QuerySoapWebService(url, "IsValidProductTypeID", pairs);
            if (doc == null) return false;
            string ret = doc.GetElementsByTagName("result")[0].InnerText;

            return Convert.ToBoolean(ret);
        }

        public bool IsValid_PIM_ZiGuang_ClientSoft(string version)
        {
            Hashtable pairs = new Hashtable();
            pairs.Add("csVersion", version);
            XmlDocument doc = m_webObj.QuerySoapWebService(url, "IsValid_PIM_ZiGuang_ClientSoft", pairs);
            if (doc == null) return false;
            string ret = doc.GetElementsByTagName("result")[0].InnerText;

            return Convert.ToBoolean(ret);
        }

        public bool PIMData_Upload(ClsDataPIMTestForMOD cResult, List<PIMTestItemInfo> lstResult)
        {
            Hashtable pairs = new Hashtable();
            pairs.Add("pi", cResult);
            pairs.Add("list", lstResult);

            XmlDocument doc = m_webObj.QuerySoapWebService(url, "PIMData_Upload", pairs);
            if (doc == null) return false;
            string ret = doc.GetElementsByTagName("result")[0].InnerText;

            return Convert.ToBoolean(ret);
        }

        public bool PIMData_SecondTest_Upload(ClsDataPIMTestForMOD cResult, List<PIMTestItemInfo> lstResult)
        {
            Hashtable pairs = new Hashtable();
            pairs.Add("pi", cResult);
            pairs.Add("list", lstResult);

            XmlDocument doc = m_webObj.QuerySoapWebService(url, "PIMData_SecondTest_Upload", pairs);
            if (doc == null) return false;
            string ret = doc.GetElementsByTagName("result")[0].InnerText;

            return Convert.ToBoolean(ret);
        }

        public bool Isvalid_ProductType_ZTE(string id)
        {
            Hashtable pairs = new Hashtable();
            pairs.Add("productTypeID", id);

            XmlDocument doc = m_webObj.QuerySoapWebService(url, "Isvalid_ProductType_ZTE", pairs);
            if (doc == null) return false;
            string ret = doc.GetElementsByTagName("result")[0].InnerText;

            return Convert.ToBoolean(ret);
        }

        public bool ZTE_PIMTestData_Upload(string id, tb_Result_DFL_Info cResult, List<tb_Param_DFL_Info> lstResult)
        {
            Hashtable pairs = new Hashtable();
            pairs.Add("ti", cResult);
            pairs.Add("productTypeID", id);
            pairs.Add("items", lstResult);

            XmlDocument doc = m_webObj.QuerySoapWebService(url, "ZTE_PIMTestData_Upload", pairs);
            if (doc == null) return false;
            string ret = doc.GetElementsByTagName("result")[0].InnerText;

            return Convert.ToBoolean(ret);
        }

        public bool ProductType_Flow_HasValidSevenStep_ForPIMTest(string productSN, string productTypeID)
        {
            Hashtable pairs = new Hashtable();
            pairs.Add("productSN", productSN);
            pairs.Add("productTypeID", productTypeID);

            XmlDocument doc = m_webObj.QuerySoapWebService(url, "ProductType_Flow_HasValidSevenStep_ForPIMTest", pairs);
            if (doc == null) return false;
            string ret = doc.GetElementsByTagName("result")[0].InnerText;

            return Convert.ToBoolean(ret);
        }
        public bool ProductType_Flow_HasValidSevenStep_ForPIMSecondTest(string productSN, string productTypeID)
        {
            Hashtable pairs = new Hashtable();
            pairs.Add("productSN", productSN);
            pairs.Add("productTypeID", productTypeID);

            XmlDocument doc = m_webObj.QuerySoapWebService(url, "ProductType_Flow_HasValidSevenStep_ForPIMSecondTest", pairs);
            if (doc == null) return false;
            string ret = doc.GetElementsByTagName("result")[0].InnerText;

            return Convert.ToBoolean(ret);
        }

        public List<string> GetFreBandByProductTypeID(string productTypeID)
        {
            List<string> freBand = new List<string>();
            Hashtable pairs = new Hashtable();
            pairs.Add("productTypeID", productTypeID);

            XmlDocument doc = m_webObj.QuerySoapWebService(url, "GetMultiBandByProductTypeID", pairs);
            if (doc == null) return null;
            XmlNodeList xnl = doc.GetElementsByTagName("TestBand");
            for (int i = 0; i < xnl.Count; i++)
            {
                string str = xnl[i].InnerText;
                if (str != string.Empty)
                    freBand.Add(str);
            }

            return freBand;
        }

        public List<UpdatatestInfo> GetLatestTestbyProductSN(string productSN)
        {
            List<UpdatatestInfo> freBandInfo = new List<UpdatatestInfo>();
            Hashtable pairs = new Hashtable();
            pairs.Add("productSN", productSN);

            XmlDocument doc = m_webObj.QuerySoapWebService(url, "GetLatestTestbyProductSN", pairs);
            if (doc == null) return null;
            XmlNodeList xnl = doc.GetElementsByTagName("PIMTest_tempInfo");
            for (int i = 0; i < xnl.Count; i++)
            {
                //testInfo t = new testInfo();
                //t.TestBand = xnl[i].ChildNodes[11].InnerText;
                //t.Flag =Convert.ToBoolean(xnl[i].ChildNodes[12].InnerText);
                UpdatatestInfo t = new UpdatatestInfo();
                t.ProductSN = xnl[i].ChildNodes[0].InnerText;
                t.TestTime = Convert.ToDateTime(xnl[i].ChildNodes[1].InnerText);
                t.ProductTypeID = xnl[i].ChildNodes[2].InnerText;
                t.UserID = xnl[i].ChildNodes[3].InnerText;
                t.PCIP = xnl[i].ChildNodes[4].InnerText;
                t.SignalSource1 = xnl[i].ChildNodes[5].InnerText;
                t.SignalSource2 = xnl[i].ChildNodes[6].InnerText;
                t.SpectrumAnalyzer = xnl[i].ChildNodes[7].InnerText;
                t.TestStep = xnl[i].ChildNodes[8].InnerText;
                t.IsPass = xnl[i].ChildNodes[9].InnerText;
                t.MoreContent = xnl[i].ChildNodes[10].InnerText;
                t.TestBand = xnl[i].ChildNodes[11].InnerText;
                t.Flag = Convert.ToBoolean(xnl[i].ChildNodes[12].InnerText);
                freBandInfo.Add(t);
            }

            return freBandInfo;
        }

        public List<PIMTestItemInfo> GetTestItembyTestInfo(TestDataInfo info, DateTime dt)
        {
            Hashtable pairs = new Hashtable();
            pairs.Add("ti", info);

            XmlDocument doc = m_webObj.QuerySoapWebService(url, "GetTestItembyTestInfo", pairs);
            if (doc == null) return null;
            List<PIMTestItemInfo> ps = new List<PIMTestItemInfo>();
            XmlNodeList xnl = doc.GetElementsByTagName("PIMTestItemInfo");

            for (int i = 0; i < xnl.Count; i++)
            {
                PIMTestItemInfo p = new PIMTestItemInfo();
                p.ProductSN = xnl[0].ChildNodes[0].InnerText;
                p.TestTime = dt;
                p.TopoAlias = xnl[0].ChildNodes[2].InnerText;
                p.InterceptOrder = xnl[0].ChildNodes[3].InnerText;
                p.InputPower1 = Convert.ToDecimal(xnl[0].ChildNodes[4].InnerText);
                p.InputPower2 = Convert.ToDecimal(xnl[0].ChildNodes[5].InnerText);
                p.InputF1 = Convert.ToDecimal(xnl[0].ChildNodes[6].InnerText);
                p.InputF2 = Convert.ToDecimal(xnl[0].ChildNodes[7].InnerText);
                p.OutputF = Convert.ToDecimal(xnl[0].ChildNodes[8].InnerText);
                p.TestValue = Convert.ToDecimal(xnl[0].ChildNodes[9].InnerText);
                p.HighSpec = Convert.ToDecimal(xnl[0].ChildNodes[10].InnerText);
                p.IsPass = xnl[0].ChildNodes[11].InnerText;
                ps.Add(p);
            }
            return ps;
        }

        public bool UploadData_TempDataBase(UpdatatestInfo info, List<PIMTestItemInfo> li, bool tag)
        {

            Hashtable pairs = new Hashtable();
            pairs.Add("ti", info);
            pairs.Add("list", li);
            pairs.Add("tag", tag);
            XmlDocument doc = m_webObj.QuerySoapWebService(url, "UploadData_TempDataBase", pairs);
            if (doc == null) return false;
            XmlNodeList xnl = doc.GetElementsByTagName("result");

            return Convert.ToBoolean(xnl[0].InnerText);
        }

        public bool Repair_AutoBuild(string productSN, string userID)
        {
            Hashtable pairs = new Hashtable();
            pairs.Add("productSN", productSN);
            pairs.Add("userID", userID);
            XmlDocument doc = m_webObj.QuerySoapWebService(url, "Repair_AutoBuild", pairs);
            if (doc == null) return false;
            XmlNodeList xnl = doc.GetElementsByTagName("result");

            return Convert.ToBoolean(xnl[0].InnerText);
        }

    }
}
